
<template>
  <!-- <el-alert :closable="false">
      <router-view />
  </el-alert>-->
  <div style="padding:30px;width:60%">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="115px"
      class="demo-ruleForm"
    >
      <el-form-item label="账号名称" prop="account" required>
        <el-col :span="22">
          <el-input v-model="ruleForm.account"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="人员姓名" prop="name">
        <el-col :span="22">
          <el-input v-model="ruleForm.name"></el-input>
        </el-col>
      </el-form-item>

      <el-form-item label="账号密码" prop="password">
        <el-col :span="22">
          <el-input type="password" v-model="ruleForm.password"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="请确认密码" prop="rppassword">
        <el-col :span="22">
          <el-input type="password" v-model="ruleForm.rppassword"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="角色" prop="role" required>
        <el-select v-model="ruleForm.role" placeholder="请选择人员角色">
          <el-option label="管理人员" value="1"></el-option>
          <el-option label="财务人员" value="2"></el-option>
          <el-option label="项目人员" value="3"></el-option>
        </el-select>
      </el-form-item>
      <!-- 这里应该有个总公司的id的，可能是默认带了 -->
      <el-form-item label="人员电话" prop="telephone">
        <el-col :span="22">
          <el-input v-model="ruleForm.telephone"></el-input>
        </el-col>
      </el-form-item>


      <el-form-item v-if="qx.level>3" label="分公司" prop="bid" >
        <el-select @change="slfgs" v-model="ruleForm.bid" placeholder="请选择分公司">
          <el-option v-for="item in coms" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>

   

      <el-form-item  v-if="qx.level>2" label="部门" prop="pid">
        <el-select @change="slbm" v-model="ruleForm.pid" placeholder="请选择部门">
          <el-option v-for="item in coms1" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
    

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { hqfgs, tjht } from "@/api/ht";
import { getToken, gettype, getid , getlevel} from "@/utils/auth";
import { tjgs } from "@/api/zuzhijiagou";
import request from "@/utils/request";
import axios from "axios";
export default {
  name: "DragTable",

  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };

    return {
      list: null,
      coms: [],
      coms1: [],
      qx:[],
      ruleForm: {
        account: "",
        aid: "",
        cid: "",
        createTime: "",
        del: "",
        id: "",
        name: "",
        password: "",
        rppassword: "",
        pid: "",
        telephone: "",
        role: ""
      },

      rules: {
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        rppassword: [
          { required: true, validator: validatePass2, trigger: "blur" }
        ],
        person: [
          {
            required: true,
            message: "请输入分公司负责人的姓名",
            trigger: "blur"
          }
        ],
        telephone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          { min: 11, max: 11, message: "请输入11手机号", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.selectdown();
    this.getqx();
  },
  methods: {
    submitForm(formName) {
      var that = this;
      this.$refs[formName].validate(valid => {
        if (valid) {
          that.ruleForm.level=getlevel()
          // 添加人员的方法
          axios.post(that.$host + "person/add", that.ruleForm, {
            headers: {
              "Content-Type": "application/json",
              token: getToken(),
              type: 1
            }
          }).then(res=>{
            if(res.data.code==1){
              that.$message.success(res.data.message)
            }
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 获取分公司列表
    selectdown() {
      var that = this;
      var p = {
        tokens: getToken()
      };
      request(`branch/selectdown`, {
        method: "get",
        headers: { "Content-Type": "application/json" },
        params: p
      })
        .then(function(response) {
          console.log(response);
          if (response.data.code == 1) {
            that.coms = response.data.object;
          } else {
            that.fail = true;
            that.failc = response.message;
          }
        })
        .catch(error => {
          console.log(error); //异常
        });
    },
    getqx() {
      var that = this;
      var p = {
        tokens: getToken()
      };
      request(`person/rolelist`, {
        method: "get",
        headers: { "Content-Type": "application/json" },
        params: p
      })
        .then(function(response) {
          console.log(response);
          if (response.data.code == 1) {
            that.qx = response.data.object;
            console.log(that.qx.level)
            var level=that.qx

            if(level==4){
              that.ruleForm.cid=level.cid
            }else if(level==3){
              that.ruleForm.cid=level.cid
              that.ruleForm.bid=level.bid
            }else if(level==2){
              that.ruleForm.cid=level.cid
              that.ruleForm.bid=level.bid
              that.ruleForm.pid=level.pid
            }
          } else {
            that.fail = true;
           
          }
        })
        .catch(error => {
          console.log(error); //异常
        });
    },
    // 获取部门列表
    selectdown1(a) {
      console.log("add");
      var that = this;
      var p = {
        tokens: getToken(),
        bid: a
      };
      request(`department/selectComboBox`, {
        method: "get",
        headers: { "Content-Type": "application/json" },
        params: p
      })
        .then(function(response) {
          console.log(response);
          if (response.data.code == 1) {
            that.coms1 = response.data.object;
          } else {
            that.fail = true;
            that.failc = response.message;
          }
        })
        .catch(error => {
          this.loading = false;
          that.fail = true;
          that.failc = response.message;
          console.log(error); //异常
        });
    },
    slfgs(selVal) {
      this.fgsid = selVal;
      console.log(selVal);
      this.selectdown1(selVal);
      // this.dialogVisible = true;
    },
    slbm(selVal) {
      this.bmid = selVal;
      // this.dialogVisible = true;
    }
  }
};
</script>

<style scoped>
.sortable-ghost {
  opacity: 0.8;
  color: #fff !important;
  background: #42b983 !important;
}
/* .el-alert__content {
  width: 300px;
} */
</style>

<style scoped>
.icon-star {
  margin-right: 2px;
}
.el-alert__content {
  width: 50%;
}
.drag-handler {
  width: 20px;
  height: 20px;
  cursor: pointer;
}
.show-d {
  margin-top: 15px;
}
/* .el-alert__content {
  width: 300px;
} */
</style>
